package my.ts.code.adapter;

import java.util.ArrayList;

import my.ts.code.objects.Item;
import my.ts.code.util.Util;
import my.ts.code.viewgroup.CategoryItem;
import my.ts.code.viewgroup.DetailItem;
import my.ts.code.viewgroup.LoadingMoreItem;
import my.ts.code.viewgroup.MoreItem;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HeaderAdapter extends ArrayAdapter<Item> {

	ArrayList<Item> myArray;
	int res;
	Context context;
	int tvr;
	String syntax;

	public HeaderAdapter(Context context, int textViewResourceId,
			ArrayList<Item> objects, String syntax) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub

		this.context = context;
		res = textViewResourceId;
		myArray = objects;
		this.tvr = textViewResourceId;
		this.syntax = syntax;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// // TODO Auto-generated method stub

		switch (tvr) {
		case 0:
			return displayCategoryType(convertView, position);

		case 1:
			return displayItemType(convertView, position);

		default:
			break;
		}

		return null;
	}

	private View displayCategoryType(View convertView, int position) {
		View itemview = convertView;

		final Item item = myArray.get(position);
		if (item != null) {
			if (item.getMore() == 0) {
				itemview = new CategoryItem(getContext());

				TextView tvName = ((CategoryItem) itemview).title;
				tvName.setText(item.getHeaderName());
			} else if (item.getMore() == 1) {
				itemview = new MoreItem(getContext());
			} else if (item.getMore() == 2) {
				itemview = new LoadingMoreItem(getContext());
			}
		}
		return itemview;
	}

	private View displayItemType(View convertView, final int position) {
		View itemview = convertView;
		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				int pos = msg.what;
				Drawable d = (Drawable) msg.obj;
				if (myArray.size() != 0) {
					myArray.get(pos).setAvatar(d);
					notifyDataSetChanged();
				}
				super.handleMessage(msg);
			}
		};

		final Item item = myArray.get(position);
		if (item != null) {
			if (item.getMore() == 0) {
				itemview = new DetailItem(getContext());

				TextView tvName = ((DetailItem) itemview).title;
				tvName.setText(item.getHeaderName());

				TextView tvHeader = ((DetailItem) itemview).header;
				if (item.getSMSDownloadCode() != null)
					tvHeader.setText("Lượt tải: " + " " + "23456");
				else
					tvHeader.setVisibility(View.GONE);

				ImageView avatar = ((DetailItem) itemview).avatar;

				if (item.getUrlThumb() != null && item.isLoaded() == false
						&& item.getAvatar() == null) {
					item.setLoaded(true);

					Thread thread = new Thread() {
						@Override
						public void run() {
							Drawable d = Util.getImage(item.getUrlThumb());

							if (d != null) {
								Message msg = handler
										.obtainMessage(position, d);
								handler.sendMessage(msg);
							}
						}
					};
					thread.start();
				} else
					avatar.setBackgroundDrawable(myArray.get(position)
							.getAvatar());
			} else if (item.getMore() == 1) {
				itemview = new MoreItem(getContext());
			} else if (item.getMore() == 2) {
				itemview = new LoadingMoreItem(getContext());
			}

		}
		return itemview;
	}
}
